<?php

namespace app\Http\Controllers\Admin;

use support\Response;


trait Tool
{
    public function success()
    {

        $response = response();
        $response->withStatus(200);
        $result = [
            "status" => "success",
            "status_code" => 200
        ];
        $response->withHeaders([
            "Content-Type" => "application/json"
        ]);
        $response->withBody(json_encode($result));
        return $response;

    }

    public function successWithInfo($info = '操作成功', $code = 200)
    {
        $response = response();
        $response->withStatus($code);
        $result = [
            "info" => $info,
            "status" => "success",
            "status_code" => 200
        ];
        $response->withHeaders([
            "Content-Type" => "application/json"
        ]);
        $response->withBody(json_encode($result));
        return $response;
    }
//
    public function successWithData($data = [], $code = 200)
    {
        return new Response(
            $code,
            ['Content-Type' => 'application/json'],
            json_encode(
                [
                    "data" => $data,
                    "status" => "success",
                    "status_code" => $code
                ],
                JSON_UNESCAPED_UNICODE
            )
        );
    }

    public function successWithRaw($data = [], $code = 200)
    {
        $response = response();
        $response->withStatus($code);
        $result = $data;
        $response->withHeaders([
            "Content-Type" => "application/json"
        ]);
        $response->withBody(json_encode($result));
        return $response;
    }
//
    public function error()
    {
        return new Response(
            404,
            ['Content-Type' => 'application/json'],
            json_encode(
                [
                    "status" => 'error',
                    "status_code" => 404
                ],
                JSON_UNESCAPED_UNICODE
            )
        );
    }
//
    public function errorWithInfo($info = '操作失败', $code = 404)
    {
        return new Response(
            $code,
            ['Content-Type' => 'application/json'],
            json_encode(
                [
                    "info" => $info,
                    "status" => "error",
                    "status_code" => $code
                ],
                JSON_UNESCAPED_UNICODE
            )
        );
    }
//
    public function errorWithData($data = [], $code = 404)
    {
        return new Response(
            $code,
            ['Content-Type' => 'application/json'],
            json_encode(
                [
                    "data" => $data,
                    "status" => "error",
                    "status_code" => $code
                ],
                JSON_UNESCAPED_UNICODE
            )
        );
    }

    public function errorWithRaw($data = [], $code = 400)
    {
        $response = response();
        $response->withStatus($code);
        $result = $data;
        $response->withHeaders([
            "Content-Type" => "application/json"
        ]);
        $response->withBody(json_encode($result));
        return $response;
    }


}
